package com.rf.richfitwheel.admin.sys.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.CodeDefMapper;
import com.rf.richfitwheel.admin.sys.dao.CodeRuleMapper;
import com.rf.richfitwheel.admin.sys.dao.SqlAdapterMapper;
import com.rf.richfitwheel.admin.sys.model.CodeDef;
import com.rf.richfitwheel.admin.sys.model.CodeRule;
import com.rf.richfitwheel.admin.sys.service.CodeRuleService;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import com.rf.richfitwheel.common.utils.UuidUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Author bw
 * Date  2019-05-05
 */
@Service
@Transactional
public class CodeRuleServiceImpl implements CodeRuleService {
    @Autowired
    private CodeRuleMapper codeRuleDao;
    @Autowired
    private CodeDefMapper codeDefDao;
    @Autowired
	private SqlAdapterMapper sqlAdapterMapper;// 注入dao
    
    
    @Override
    public CodeRule get(String id){
        return codeRuleDao.get(id);
    }
    
    @Override
    public List<CodeRule> findList(CodeRule codeRule) {
        return codeRuleDao.findList(codeRule);
    }
    
    @Override
    public List<CodeRule> findAllList() {
        return codeRuleDao.findAllList();
    }
    
    //插入编码规则记录
    @Override
    public R insert(CodeRule codeRule) {
    	try {
    		String id = UuidUtil.get32UUID();
    		String code = codeRule.getCode();
    		codeRule.setId(id);
    		if("3".equals(codeRule.getKind())) {
    			//查询是否已经含有流水号，只允许添加一个流水号
    			List<CodeRule> codeRuleList = codeRuleDao.selectListByKindAndCode(codeRule);
    			if(codeRuleList!=null&&codeRuleList.size()>0) {
    				return R.error("已经包含流水号，只允许添加一个流水号");
    			}
    			codeRule.setRule("SN");
    		}
    		codeRule.setCreatetime(new Date());    		
    		codeRuleDao.insert(codeRule);
    		return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
    }
    
    @Override
    public int insertBatch(List<CodeRule> codeRules){
        return codeRuleDao.insertBatch(codeRules);
    }
    
    @Override
    public R update(CodeRule codeRule) {
        try {
        	if("3".equals(codeRule.getKind())) {
        		//如果已经有流水号，不允许将别的变为流水号
        		List<CodeRule> codeRuleList = codeRuleDao.selectListByKindAndCode(codeRule);
    			if(codeRuleList!=null&&codeRuleList.size()>0) {
    				if(!codeRule.getId().equals(codeRuleList.get(0).getId())) {
    					return R.error("已经包含流水号，只允许添加一个流水号");
    				}
    			}
    			codeRule.setRule("SN");
    		}
			codeRuleDao.update(codeRule);
			return R.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
    }
    
    @Override
    public R delete(String[] ids) {
    	try {
			if(ids!=null && ids.length>0) {
				for (String id : ids) {
					codeRuleDao.delete(id);
				}
				return R.ok();
			}else {
				return R.error();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
    }

    //查询分页数据
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		try {
			String codeDefId = (String) params.get("id");
			String code = "";
			if(codeDefId!=null && !"".equals(codeDefId)) {
				CodeDef codeDef = codeDefDao.selectByPrimaryKey(codeDefId);//
				code=codeDef.getCode();
			}

			PageUtils pageUtils = PageUtils.getPageInfo(params);
			Page<Object> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
			StringBuffer sql = new StringBuffer(
					"select r.id,r.code,r.name,r.kind,r.rule,r.position,r.snloop,r.snsize,"
					+ "r.snmin,r.snmax,r.notes,r.version,r.snfixlen,r.divisor from sys_code_rule r ");
			CodeRule codeRule = (CodeRule) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")),
					CodeRule.class);
			
			if(!"".equals(code)){
				sql.append(" where r.code = '"+code+"'");
			}
			sql.append(" order by position,createtime");
//			if(delegation!=null){
//				sql.append(" where 1=1");
//			}
			// sql.append(" order by class_code,data_code ");
			sqlAdapterMapper.selectSQL(sql.toString());
			pageUtils = new PageUtils(p);
			List<Map<String, String>> mapList = (List<Map<String, String>>) pageUtils.getList();
			
			pageUtils.setList(mapList);
			return pageUtils;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
